package com.cmcc.passenger.ui.profile

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.cmcc.passenger.R
import com.cmcc.passenger.config.PassPath
import com.cmcc.passenger.data.entity.FavoriteAddress
import com.cmcc.passenger.databinding.ListItemAddressBinding
import com.cmcc.passenger.databinding.ListItemAddressTitleBinding
import com.cmcc.shared.utils.clickWithTrigger

class AddressAdapter : ListAdapter<FavoriteAddress, RecyclerView.ViewHolder>(MyItemDiffCallback()) {

    private var context: Context? = null
    private var onDeleteClickListener: OnDeleteClickListener? = null
    private var onItemClickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        context = parent.context
        if (viewType == R.layout.list_item_address) {
            return AddressViewHolder(
                ListItemAddressBinding.inflate(
                    LayoutInflater.from(
                        context
                    ), parent, false
                )
            )
        } else {
            return AddressTitleViewHolder(
                ListItemAddressTitleBinding.inflate(
                    LayoutInflater.from(
                        context
                    ), parent, false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val currentType = getItemViewType(position)
        val currentData = getItem(position)


        if (currentType == R.layout.list_item_address_title && holder is AddressTitleViewHolder)
        {
            if(position!=currentList.size-1){
                val currentTypeNext =   getItemViewType(position+1)

                if (currentTypeNext == R.layout.list_item_address_title){

                    holder.binding.addAddress.visibility = View.VISIBLE
                }
                else{
                    if(!(currentData.category?.equals(context?.resources?.getString(R.string.favorite_address_title))!!)){
                        holder.binding.addAddress.visibility = View.GONE
                    }
                    else{
                        holder.binding.addAddress.visibility = View.VISIBLE
                    }

                }
            }


                holder.binding.title.text = currentData.category
            holder.binding.addAddress.clickWithTrigger {
                ARouter.getInstance()
                    .build(PassPath.DIRECTION_SEARCH)
                    .withBoolean("address_book", true)
                    .withInt("request_code", getRequestByCategory(currentData.category))
                    .navigation(
                        context as Activity,
                        getRequestByCategory(currentData.category)
                    )
            }
        } else if (currentType == R.layout.list_item_address && holder is AddressViewHolder) {
            holder.binding.address.text = currentData.name
            holder.binding.delete.clickWithTrigger {
                onDeleteClickListener?.onClick(currentData)
            }
            holder.itemView.clickWithTrigger {
                onItemClickListener?.onClick(currentData)
            }
        }
    }

    fun setOnDeleteClickListener(listener: OnDeleteClickListener) {
        this.onDeleteClickListener = listener
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        onItemClickListener = listener
    }

    private fun getRequestByCategory(category: String?): Int {
        return when (category) {
            context?.getString(R.string.home_address_title) -> AddressBookActivity.REQUEST_ADD_HOME_CODE
            context?.getString(R.string.company_address_title) -> AddressBookActivity.REQUEST_ADD_COMPANY_CODE
            context?.getString(R.string.favorite_address_title) -> AddressBookActivity.REQUEST_ADD_FAVORITY_CODE
            else -> AddressBookActivity.REQUEST_ADD_FAVORITY_CODE
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (currentList[position].category != null)
            R.layout.list_item_address_title
        else
            R.layout.list_item_address
    }

    interface OnDeleteClickListener {
        fun onClick(address: FavoriteAddress)
    }

    interface OnItemClickListener {
        fun onClick(address: FavoriteAddress)
    }

}


class MyItemDiffCallback : DiffUtil.ItemCallback<FavoriteAddress>() {
    override fun areItemsTheSame(oldItem: FavoriteAddress, newItem: FavoriteAddress): Boolean {
        return oldItem == newItem
    }

    override fun areContentsTheSame(oldItem: FavoriteAddress, newItem: FavoriteAddress): Boolean {
        return oldItem == newItem
    }

}